import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier as KNN
print("正在读取数据...")
unicom = pd.read_excel('data/beijing_haidian.xlsx', sheet_name=0, usecols=[0, 2, 3, 6, 7], names=["datetime","longitude","latitude","internet_in","internet_out"])
unicom = unicom.set_index('datetime')
unicom['internet'] = unicom['internet_in']+unicom['internet_out']
print("正在去重...")
unicom1 = unicom.drop_duplicates(subset = ['longitude', 'latitude'])

lon_lag=(116.3812-116.2675)/195
lat_lag=(39.7990-39.7091)/200
print("开始划分栅格...")
lon=[]
lat=[]
for i in range(195):
    print("第"+str(i+1)+"行ing...")
    for j in range(200):
        l1=116.2675+0.5*lon_lag+j*lon_lag
        l2=39.7091+0.5*lat_lag+i*lat_lag
        lon=np.append(lon,l1)
        lat=np.append(lat,l2)
print("划分栅格完成啦！")
list1=unicom1['longitude'].values
list2=unicom1['latitude'].values
train_list=list(zip(list1,list2))
train_list=np.array(train_list)

a=list(range(1,172))
a=np.array(a)

X = train_list   #训练集
y = a                    #标签
test=list(zip(lon , lat))    #测试集

neigh = KNN(n_neighbors=1)
neigh.fit(X, y)
label=neigh.predict(test)
print("打标签完成啦！")
df=pd.DataFrame(lon, columns=['longitude'])
df['latitude']= lat
df['bs_label']= label
print("保存文件ing...")
df.to_csv('data.txt')
print("finished!")